package it.zqdszb.library.api;


import android.util.Log;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import it.zqdszb.MyApplication;
import it.zqdszb.library.retrofit.Api.BaseApi;
import it.zqdszb.library.utils.utils.SpUtil;
import retrofit2.Retrofit;
import rx.Observable;


/**
 * Created by wcy on 2017/3/14.
 */
public class SubjectPostApi extends BaseApi {
    private Map <String, Object>parameters;

    private List <String>  paramsList=new ArrayList<>();
    public Map getParameters() {
        return parameters;
    }

    public  HashMap<String, Integer> mapMethods ;

    /**
     * 参数设置
     *
     * @param method     请求指令
     * @param parameters 请求参数
     */
    public void setParameters(String method, Map parameters) {
        super.setMethod(method);
        this.parameters = parameters;
        if( this.parameters!=null){
            for (Map.Entry<String, Object> entry : this.parameters.entrySet()) {
                paramsList.add(entry.getValue().toString());
            }
        }
    }

    @Override
    public Observable getObservable(Retrofit retrofit) {
        Object httpService = retrofit.create(HttpPostService.class);
        try {

            if(MyApplication.getInstance().start){
                MyApplication.getInstance().start=false;
                Method[] methods = httpService.getClass().getMethods();
                mapMethods= new HashMap<>();
                for (Method method : methods) {
                    Class<?>[] parameterTypes = method.getParameterTypes();
                    mapMethods.put(method.getName(), parameterTypes.length);
                }
                Log.e("666", "getObservable: 5555");
                SpUtil.putMap(MyApplication.getInstance().getApplicationContext(),"mapMethods",mapMethods);
            }else{
                Log.e("666", "getObservable: 6666");
                mapMethods=SpUtil.getMap(MyApplication.getInstance().getApplicationContext(),"mapMethods");
            }

          /*  String params = "";
            String token = JConstant.getToken();
            if (parameters != null) {
                if (JConstant.isEncrypt()) {
                    params = AESOperator.encrypt(parameters.toJSONString());
                } else {
                    params = parameters.toJSONString();
                }
            }*/
          //目前设置get请求
            if(isGetHttp()){
                int count = mapMethods.get(getMethod());
                if(count==0){
                    return (Observable) httpService.getClass()
                            .getMethod(getMethod())
                            .invoke(httpService);
                }else if(count==1){
                    return (Observable) httpService.getClass()
                            .getMethod(getMethod(), new Class[]{String.class})
                            .invoke(httpService,paramsList.get(0).toString());
                }else if(count==2){
                      return (Observable) httpService.getClass()
                              .getMethod(getMethod(), new Class[]{String.class, String.class})
                              .invoke(httpService, paramsList.get(0).toString(), paramsList.get(1).toString());
                }else if(count==3){
                    return (Observable) httpService.getClass()
                            .getMethod(getMethod(), new Class[]{String.class, String.class, String.class})
                            .invoke(httpService, paramsList.get(0).toString(), paramsList.get(1), paramsList.get(2));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
